package paa.crawlers.delicious;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PopularTagBasedUserFinder {

	public Set<String> findUsers(int numberOfRelatedPages) throws IOException {
		BufferedReader in = null;
		Set<String> users = new HashSet<String>();

		String urlBase = "http://del.icio.us/tag/";
		String urlPopularTags = urlBase + "?sort=freq";
		
		Set<String> popularTags = new HashSet<String>();
		URL delicious = new URL(urlPopularTags);
		URLConnection dc = delicious.openConnection();
		
		//Pegando tags populares
		String inputLine;
		try {
			in = new BufferedReader(new InputStreamReader(dc.getInputStream()));
			
			//		<a href="/tag/design" class="r s5">design</a>
			//		<a href="/tag/webdev" class="lb s2">webdev</a>
			Pattern pTags = Pattern.compile("(\\s*<a href=\"/tag.*>)(.*)(</a>)");
			
			while ((inputLine = in.readLine()) != null)	{
				Matcher matcher = pTags.matcher(inputLine);
				
				if (matcher.matches()) {
					System.out.println("Found Tag: " + matcher.group(2));
					popularTags.add(matcher.group(2));
				}
			}
			in.close();
		} finally {
			if (in != null) in.close();
		}
		
		//Crawling tag pages
		Set<String> tagsToCrawl = popularTags;
		for (int i = 0; i < numberOfRelatedPages; i++) {
			Set<String> relatedTags = new HashSet<String>();
			try {
				
				// <div class="commands"> &nbsp;<a save href="/flaviovdf?url=http%3A%2F%2Fwww.linearpublishing.com%2FRhinoStory.html&amp;title=J.K.%20Rowling%2C%20Lexicon%20and%20Oz&amp;copyuser=jamiep39&amp;copytags=2008&amp;jump=&amp;partner=del" class="copy" rel="nofollow">save this</a></div> <div class="meta">by <a href="/jamiep39" class="user">jamiep39</a> to <a class="tag" href="/jamiep39/2008">2008</a> ... <a class="pop" href="/url/dc804dc0cea28f8febe0ef7902be295a" style="padding: 0 0.2em; background-color: rgb(100%, 77%, 77%);">saved by 108 other people</a> <span class="date" title="2008-05-06T17:11:10Z">... 2 mins ago</span> </div>
				Pattern pLink = Pattern.compile("(\\s*.*class=\"user\">)(.*)(</a>\\s*to\\s*<a)(.*)");
				
				//		<li><a href="/tag/obama">obama</a></li>
				Pattern pRelatedTags = Pattern.compile("(\\s*<li><a href=\"/tag.*>)(.*)(</a></li>)");
				
				for (String tag : tagsToCrawl) {
					System.out.println("Crawling tag " + tag);
					URL urlTag = new URL(urlBase + tag + "?setcount=100");
					dc = urlTag.openConnection();
					
					in = new BufferedReader(new InputStreamReader(dc.getInputStream()));
					while ((inputLine = in.readLine()) != null)	{
						Matcher linkMatcher = pLink.matcher(inputLine);
						Matcher relatedMatcher = pRelatedTags.matcher(inputLine);
						
						if (linkMatcher.matches()) {
							System.out.println("Found user: " + linkMatcher.group(2));
							users.add(linkMatcher.group(2));
						}
						
						if (relatedMatcher.matches()) {
							System.out.println("Found related tag: " + relatedMatcher.group(2));
							relatedTags.add(relatedMatcher.group(2));
						}
					}
					
					Thread.sleep(2000);
					in.close();
				}
			} catch (Exception e) {
				System.out.println("Exception occured: " + e);
			} finally {
				if (in != null)  in.close();
			}
			
			tagsToCrawl = relatedTags;
		}
		
		return users;
	}
}